package com.TeacherInfoServer.mapper;


import com.TeacherInfoServer.pojo.DTO.PositionHistoryDTO;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.PositionHistorySearchDTO;
import com.TeacherInfoServer.pojo.entity.PositionHistory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface PositionHistoryMapper extends BaseMapper<PositionHistory> {
    @Select("SELECT ph.id, d.name as departmentName, t.teacher_name as teacherName, ph.start_date, ph.end_date, ty.value as positionType " +
            "FROM position_history ph " +
            "LEFT JOIN department d ON ph.department_id = d.id " +
            "LEFT JOIN teacher t ON ph.teacher_id = t.teacher_id " +
            "LEFT JOIN type_info ty ON ph.position_type = ty.id "
    )
    Page<PositionHistoryDTO> getPositionListByPage(Page<?> page,@Param("qw")QueryWrapper<?> queryWrapper);

    @Select("SELECT ph.id, d.name as departmentName, t.teacher_name as teacherName, ph.start_date, ph.end_date, ty.value as positionType " +
            "FROM position_history ph " +
            "LEFT JOIN department d ON ph.department_id = d.id " +
            "LEFT JOIN teacher t ON ph.teacher_id = t.teacher_id " +
            "LEFT JOIN type_info ty ON ph.position_type = ty.id "
    )
    List<PositionHistoryDTO> getPositionDTOs();

    @Select("<script>" +
            "SELECT ph.id, d.name as departmentName, t.teacher_name as teacherName, ph.start_date, ph.end_date, ty.value as positionType " +
            "FROM position_history ph " +
            "LEFT JOIN department d ON ph.department_id = d.id " +
            "LEFT JOIN teacher t ON ph.teacher_id = t.teacher_id " +
            "LEFT JOIN type_info ty ON ph.position_type = ty.id " +
            "<where> " +
            "<if test='search.departmentName != null and search.departmentName.trim() != \"\"'> " +
            "AND d.name LIKE CONCAT('%', #{search.departmentName}, '%') " +
            "</if> " +
            "<if test='search.startDate != null and search.startDate.trim() != \"\"'> " +
            "AND ph.start_date LIKE CONCAT('%', #{search.startDate}, '%') " +
            "</if> " +
            "<if test='search.endDate != null and search.endDate.trim() != \"\"'> " +
            "AND ph.end_date LIKE CONCAT ('%', #{search.endDate}, '%') " +
            "</if> " +
            "<if test='search.teacherName != null and search.teacherName.trim() != \"\"'> " +
            "AND t.teacher_name LIKE CONCAT('%', #{search.teacherName}, '%') " +
            "</if> " +
            "<if test='search.positionType != null and search.positionType.trim() != \"\"'> " +
            "AND ty.value LIKE CONCAT('%', #{search.positionType}, '%') " +
            "</if> " +
            "</where>" +
            "</script>")
    Page<PositionHistoryDTO> searchPositionHistoryList(Page<?> page, @Param("search") PositionHistorySearchDTO search);
}

